package cn.edu.lingnan.controllerV1;

import cn.edu.lingnan.service.QuestionStoreService;
import cn.edu.lingnan.user.dtos.ListStoresDto;
import cn.edu.lingnan.user.dtos.QuestionStoreDTO;
import cn.edu.lingnan.user.dtos.Result;
import cn.edu.lingnan.user.pojos.PageBean;
import cn.edu.lingnan.user.pojos.QuestionStore;
import cn.edu.lingnan.user.pojos.QuestionStoreType;
import cn.edu.lingnan.user.vos.QuestionStoreVO;
import cn.edu.lingnan.user.vos.QuestionWithOptionsVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/questions")
public class QuestionStoreController {

    @Autowired
    private QuestionStoreService questionStoreService;

    /**
     * 分页条件查询题库
     * @param listStoresDto
     * @return
     */
    @PostMapping("/list")
    public Result<PageBean> listStoresByUser(@RequestBody ListStoresDto listStoresDto) {
        PageBean result =  questionStoreService.getStoresByUserId(listStoresDto);
        return Result.success(result);
    }

    //根据题库id查询题库
    @GetMapping("/get_questions")
   public Result<QuestionStore> getQuestionsByStoreId(@RequestParam("storeId") Integer storeId) {
         QuestionStore result = questionStoreService.getStoreById(storeId);
         return Result.success(result);
    }
    /**
     * 查询题库类型
     *
     * @return
     */
    @GetMapping("/types")
    public Result<List<QuestionStoreType>> getStoreTypes() {
        List<QuestionStoreType> result = questionStoreService.getStoreTypes();
        return Result.success(result);
    }

    @PostMapping("/add")
    public  Result<Boolean> addStore(@RequestBody QuestionStoreDTO dto) {
        return Result.success(questionStoreService.addStore(dto));
    }

    @PostMapping("/update")
    public Result<Boolean> updateStore(@RequestBody QuestionStore store) {
        return Result.success(questionStoreService.updateStore(store));
    }

    @DeleteMapping("/delete")
    public Result<Boolean> deleteStore(@RequestParam("storeId") Integer storeId) {
        Integer flag = questionStoreService.deleteStore(storeId);
        if (flag == 0) {
            return Result.error("删除失败,题库中有题目");
        }
        return Result.success();
    }

    @GetMapping("/{storeId}/questions")
    public ResponseEntity<List<QuestionWithOptionsVO>> getQuestions(@PathVariable Integer storeId) {
        return ResponseEntity.ok(questionStoreService.getQuestionsByStoreId(storeId));
    }
}
